//
//  AddInfoViewController.m
//  test
//
//  Created by 朱圳 on 2018/1/15.
//  Copyright © 2018年 朱圳. All rights reserved.
//

#import "AddInfoViewController.h"
#import "StuUserCell.h"
#import "UserModel.h"
#import "UserManager.h"
#import "ClockFoot.h"
#import "ZTabBarController.h"
#import "BRPickerView.h"
#import "UITextField+Limit.h"
@interface AddInfoViewController ()<UITableViewDelegate,UITableViewDataSource,UIGestureRecognizerDelegate>
@property(nonatomic,strong)UITableView *tableView;
@property(nonatomic,strong)NSMutableArray *list;
@property(nonatomic,strong)UserModel *model;
@property(nonatomic,strong)UITextField *tempTextField;

@end

@implementation AddInfoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.model = [UserManager me];
    NSString *path = [[NSBundle mainBundle] pathForResource:@"AddU.plist" ofType:nil];
    self.list =[NSMutableArray arrayWithContentsOfFile:path];
    [self.view addSubview:self.tableView];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

#pragma mark -给每个cell中的textfield添加事件，只要值改变就调用此函数
-(void)changedTextField:(id)sender
{
    UITextField *tempTextField = (UITextField *)sender;
    self.tempTextField = tempTextField;
    NSLog(@"tag------%ld;值是---%@",tempTextField.tag,tempTextField.text);
    switch (tempTextField.tag) {
        case 0:
            break;
        case 1:
            break;
        case 2:
            self.model.college = tempTextField.text;
            break;
        case 3:
            break;
        case 4:
            self.model.address = tempTextField.text;
            break;
        case 5:
//            self.model.QQ = tempTextField.text;;
            break;
        case 6:
//            self.model.Winxin = tempTextField.text;
            break;
        case 7:
            self.model.goal = tempTextField.text;
            break;
        case 8:
            self.model.QQ = tempTextField.text;;
            break;
        case 9:
            self.model.Winxin = tempTextField.text;
            break;
        case 10:
            self.model.BirthDay = tempTextField.text;
            [tempTextField resignFirstResponder];
            break;
        case 11:
            self.model.dadphone = tempTextField.text;
            break;
        case 12:
            self.model.mumphone = tempTextField.text;
            break;
        case 13:
            self.model.Baiduyun = tempTextField.text;
            break;
        case 14:
            self.model.Vyan = tempTextField.text;
            break;
            
        default:
            break;
    }
}
#pragma mark - 点击事件
-(void)tapGesture:( UITapGestureRecognizer *)sender{
    [self.view endEditing:YES];
    [BRStringPickerView showStringPickerWithTitle:@"性别" dataSource:@[@"男", @"女"] defaultSelValue:@"男" isAutoSelect:YES resultBlock:^(id selectValue) {
        NSLog(@"%@", selectValue);
        if ([selectValue isEqualToString:@"男"]) {
            self.model.sex = @"2";
            NSLog(@">>>>>>>>%@",self.model.sex);
        }else{
            self.model.sex = @"1";
        }
        [self.tableView reloadData];
    }];
}
-(void)tapGesture2:( UITapGestureRecognizer *)sender{
    [self.view endEditing:YES];
    NSDateFormatter *formattter = [[NSDateFormatter alloc]init];
    formattter.dateFormat = @"yyyy-MM-dd HH:mm:ss";
    NSString * date = [formattter stringFromDate:[NSDate date]];
    [BRDatePickerView showDatePickerWithTitle:@"出生年月" dateType:UIDatePickerModeDate defaultSelValue:@"" minDateStr:@"1980-01-01 00:00:00" maxDateStr:date isAutoSelect:YES resultBlock:^(NSString *selectValue) {
        self.model.BirthDay = selectValue;
        [self.tableView reloadData];
    }];
}
//tap5Action:
-(void)tap5Action:(UITapGestureRecognizer *)send{
    [self.view endEditing:YES];
    __weak typeof(self) weakslf = self;
    [BRStringPickerView showStringPickerWithTitle:@"数学" dataSource:@[@"数一", @"数二", @"数三", @"396", @"199", @"无"] defaultSelValue:@"数一" isAutoSelect:YES resultBlock:^(id selectValue) {
        self.model.math = selectValue;
        [weakslf.tableView reloadData];
    }];
}
//tap6Action
-(void)tap6Action:(UITapGestureRecognizer *)send{
    [self.view endEditing:YES];
    __weak typeof(self) weakslf = self;
    [BRStringPickerView showStringPickerWithTitle:@"英语" dataSource:@[@"英一", @"英二", @"日语", @"其他"] defaultSelValue:@"英一" isAutoSelect:YES resultBlock:^(id selectValue) {
        self.model.english = selectValue;
        [weakslf.tableView reloadData];
    }];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.list.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *CellIdentifier = @"stuUserCell";
    StuUserCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    if (cell == nil) {
        cell = [[StuUserCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.nameLabel.text = self.list[indexPath.row][@"name"];
    cell.tximg.image = [UIImage imageNamed:self.list[indexPath.row][@"img"]];
    cell.dataText.tag = indexPath.row;
    [cell.dataText addTarget:self action:@selector(changedTextField:) forControlEvents:UIControlEventEditingChanged];
    
    UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapGesture:)];
    tap.numberOfTouchesRequired = 1; //手指数
    tap.numberOfTapsRequired = 1; //tap次数
    tap.delegate= self;
    
    UITapGestureRecognizer *tap2=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapGesture2:)];
    tap2.numberOfTouchesRequired = 1; //手指数
    tap2.numberOfTapsRequired = 1; //tap次数
    tap2.delegate= self;
    
    //数学
    UITapGestureRecognizer *tap5=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap5Action:)];
    tap5.numberOfTapsRequired=1;
    tap5.numberOfTouchesRequired=1;
    tap5.delegate=self;
    //英语
    UITapGestureRecognizer *tap6=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap6Action:)];
    tap6.numberOfTapsRequired=1;
    tap6.numberOfTouchesRequired=1;
    tap6.delegate=self;
    

    if (indexPath.row == 0) {
        cell.dataText.text = self.model.name;
        [cell.dataText setUserInteractionEnabled:NO];
    }
    if (indexPath.row == 1) {
        if ([self.model.sex isEqualToString:@"1"]) {
            cell.dataText.text = @"女";
        }else{
            cell.dataText.text = @"男";
        }
        [cell.dataText addGestureRecognizer:tap];
    }
    if (indexPath.row == 2) {
        
        cell.dataText.text = self.model.college;
    }
    if (indexPath.row == 3) {
        cell.dataText.text = self.model.jieshu;
        [cell.dataText setUserInteractionEnabled:NO];
    }
    if (indexPath.row == 4) {
        cell.dataText.text = self.model.address;
    
    }
    if (indexPath.row == 5) {
        cell.dataText.text = self.model.math;
        [cell.dataText addGestureRecognizer:tap5];
    }
    if (indexPath.row == 6) {
        cell.dataText.text = self.model.english;
        [cell.dataText addGestureRecognizer:tap6];
    }
    if (indexPath.row == 7) {
        cell.dataText.text = self.model.goal;
    }
    if (indexPath.row == 8) {
        cell.dataText.text = self.model.QQ;
    }
    if (indexPath.row == 9) {
        cell.dataText.text = self.model.Winxin;
    }
    if (indexPath.row == 10) {
        cell.dataText.text = self.model.BirthDay;
        [cell.dataText addGestureRecognizer:tap2];
    }
    if (indexPath.row == 11) {
        cell.dataText.text = self.model.dadphone;
        [cell.dataText setLimitTextLength:11];
    }
    if (indexPath.row == 12) {
        cell.dataText.text = self.model.mumphone;
        [cell.dataText setLimitTextLength:11];
    }
    if (indexPath.row == 13) {
        cell.dataText.text = self.model.Baiduyun;
        [cell.dataText setLimitTextLength:18];
    }
    if (indexPath.row == 14) {
        cell.dataText.text = self.model.Vyan;
        [cell.dataText setLimitTextLength:18];
    }
    return cell;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    UILabel *la = [[UILabel alloc ]init ];
    la.numberOfLines = 0;
    la.frame = CGRectMake(0, 0, ScreenWidth , 40);
    la.text = @"----完善个人信息----";
    la.font = [UIFont systemFontOfSize:16.f];
    la.textAlignment = NSTextAlignmentCenter;
    
    UILabel *la2 = [[UILabel alloc ]init ];
    la2.numberOfLines = 0;
    la2.textColor = [UIColor redColor];
    la2.text = @"*只有此次完善机会，后续修改请联系教务老师！";
    la2.font = [UIFont systemFontOfSize:12.f];
    la2.textAlignment = NSTextAlignmentCenter;
    la2.frame = CGRectMake(0, 20, ScreenWidth , 40);
    [la addSubview:la2];
    return la;
}
/** 区foot视图 */
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    
    ClockFoot *foot = [[ClockFoot alloc] initWithReuseIdentifier:@"foot"];
    [ foot.btn setTitle:@"保存" forState:UIControlStateNormal];
    foot.ClickBtnHandle = ^{
        [self.view endEditing:YES];
        if(self.model.sex==nil){
            self.model.sex=@"1";
        }
        if(self.model.college==nil){
            self.model.college=@" ";
        }
        if(self.model.jieshu==nil){
            self.model.jieshu=@" ";
        }
        if(self.model.address==nil){
            self.model.address=@" ";
        }
        if(self.model.math==nil){
            self.model.math=@" ";
        }
        if(self.model.english==nil){
            self.model.english=@" ";
        }
        if(self.model.goal==nil){
            self.model.goal=@" ";
        }
        if(self.model.QQ==nil){
            self.model.QQ=@" ";
        }
        if(self.model.Winxin==nil){
            self.model.Winxin=@" ";
        }
        if(self.model.BirthDay==nil){
            self.model.BirthDay=@" ";
        }
        if(self.model.dadphone==nil){
            self.model.dadphone=@" ";
        }
        if(self.model.mumphone==nil){
            self.model.mumphone=@" ";
        }
        if(self.model.Baiduyun==nil){
            self.model.Baiduyun=@" ";
        }
        if(self.model.Vyan==nil){
            self.model.Vyan=@" ";
        }
        NSDictionary *dict = @{
                               @"id" : Sid,
                               @"password" : Spassword,
                               @"name" : self.model.name,
                               @"sex" : self.model.sex,
                               @"college" : self.model.college,
                               @"jieshu" : self.model.jieshu,
                               @"goal" : self.model.goal,
                               @"qnumber" : self.model.QQ,
                               @"wnumber" : self.model.Winxin,
                               @"birthday" : self.model.BirthDay,
                               @"dadphone" : self.model.dadphone,
                               @"mumphone" : self.model.mumphone,
                               @"bdcloud" : self.model.Baiduyun,
                               @"vnumber" : self.model.Vyan,
                               @"address":self.model.address,
                               @"math":self.model.math,
                               @"english":self.model.english,
                               };

       NSLog(@"%@>>>>>dict",dict);
        [[AFNetworkTool shareManager]postJSONWithUrl:Studen_Update parmas:dict successData:^(id json) {
            [UserManager save:self.model];
            [Utility showSuccess:@"信息已经完善"];
            ZTabBarController *tabBarControllerConfig = [[ZTabBarController alloc] init];
            [[UIApplication sharedApplication].delegate.window setRootViewController:tabBarControllerConfig];
        } failure:^(NSError *error) {
            NSLog(@"%@", error);
        }];
    };
    return foot;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 40;
}
/*******设置高度解决Button点击没效果*******/
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    
    return 50;
}

-(UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)style:UITableViewStyleGrouped];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.backgroundColor = [UIColor whiteColor];
        [_tableView registerClass:[StuUserCell class] forCellReuseIdentifier:@"stuUserCell"];
        [_tableView registerClass:[ClockFoot class] forHeaderFooterViewReuseIdentifier:@"foot"];
        _tableView.delegate = self;
        _tableView.dataSource = self;
    }
    return _tableView;
}
@end
